package com.yundasys.lab10designpattern.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * @PACKAGE_NAME: com.yundasys.lab10designpattern.observer
 * @NAME: Subject
 * @USER: jiangta
 * @DATE: 2021/11/4
 * @TIME: 9:12 上午
 * @PROJECT_NAME: springboot-labs
 */
public class Subject {

    private List<Observer> observerList = new ArrayList<>();

    private int state;

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
        notifyAllObservers();
    }

    //注册观察者
    public void attach(Observer observer) {
        observerList.add(observer);
    }
    //通知观察者们
    public void notifyAllObservers() {
        for(Observer observer:observerList){
            observer.update();
        }
    }
}
